import 'package:flutter/material.dart';

/// 患者详情页面
class PatientDetailPage extends StatefulWidget {
  final String bed;
  final String name;
  final String gender;
  final String age;
  final String id;
  final String? diagnosis;
  
  // 新增字段
  final String admissionTime;
  final String department;
  final String ward;
  final String careLevel;
  final String doctor;
  final String nurse;

  const PatientDetailPage({
    super.key,
    required this.bed,
    required this.name,
    required this.gender,
    required this.age,
    required this.id,
    this.diagnosis,
    this.admissionTime = '2025-03-30 09:00 (3天)',
    this.department = '心血管内科',
    this.ward = '心血管内科二病区',
    this.careLevel = '一级护理',
    this.doctor = '郭医生',
    this.nurse = '妙护士',
  });

  @override
  State<PatientDetailPage> createState() => _PatientDetailPageState();
}

class _PatientDetailPageState extends State<PatientDetailPage> {
  // 是否展开显示更多信息
  bool _isExpanded = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0xFF1E88E5), Color(0xFF0099FF)],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
          ),
        ),
        title: const Text('病人详情'),
        centerTitle: true,
        actions: [
          IconButton(
            icon: const Icon(Icons.person_add_outlined),
            onPressed: () {
              // 添加关注功能
            },
          ),
        ],
      ),
      body: Column(
        children: [
          // 患者基本信息区域
          _buildPatientInfoCard(),
          // 费用信息区域
          _buildCostInfoCard(),
          // 功能按钮区域
          Expanded(
            child: _buildFunctionButtons(),
          ),
        ],
      ),
    );
  }

  /// 构建患者基本信息卡片
  Widget _buildPatientInfoCard() {
    return Container(
      margin: const EdgeInsets.all(16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 床号
              Container(
                width: 60,
                height: 60,
                decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.blue.withOpacity(0.3),
                      spreadRadius: 1,
                      blurRadius: 4,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                alignment: Alignment.center,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      widget.bed.split('床')[0],
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Text(
                      '床',
                      style: TextStyle(color: Colors.white, fontSize: 12),
                    ),
                  ],
                ),
              ),
              const SizedBox(width: 16),
              // 患者信息
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Text(
                          '${widget.name} ',
                          style: const TextStyle(fontWeight: FontWeight.bold),
                        ),
                        Text(
                          '${widget.gender} ${widget.age}',
                          style: const TextStyle(
                            color: Colors.grey,
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 6),
                    Text(
                      widget.id,
                      style: const TextStyle(
                        color: Colors.grey,
                        fontSize: 12,
                      ),
                    ),
                    if (widget.diagnosis != null) ...[  
                      const SizedBox(height: 6),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const Text(
                            '诊断：',
                            style: TextStyle(color: Colors.grey, fontSize: 12),
                          ),
                          Expanded(
                            child: Text(
                              widget.diagnosis!,
                              style: const TextStyle(fontSize: 12),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ],
                ),
              ),
              // 展开/收起按钮
              GestureDetector(
                onTap: () {
                  setState(() {
                    _isExpanded = !_isExpanded;
                  });
                },
                child: Container(
                  padding: const EdgeInsets.all(4),
                  child: Icon(
                    _isExpanded ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                    color: Colors.grey,
                    size: 20,
                  ),
                ),
              ),
            ],
          ),
          // 展开的额外信息
          if (_isExpanded) ...[  
            const SizedBox(height: 12),
            const Divider(height: 1, thickness: 1, color: Color(0xFFEEEEEE)),
            const SizedBox(height: 12),
            _buildExpandedInfo(),
          ],
        ],
      ),
    );
  }
  
  /// 构建展开后显示的额外信息
  Widget _buildExpandedInfo() {
    return Column(
      children: [
        _buildInfoRow('入院时间：', widget.admissionTime),
        const SizedBox(height: 8),
        _buildInfoRow('入院科室：', widget.department),
        const SizedBox(height: 8),
        _buildInfoRow('病区：', widget.ward),
        const SizedBox(height: 8),
        _buildInfoRow('护理级别：', widget.careLevel),
        const SizedBox(height: 8),
        _buildInfoRow('责任医师：', widget.doctor),
        const SizedBox(height: 8),
        _buildInfoRow('责任护士：', widget.nurse),
      ],
    );
  }
  
  /// 构建信息行
   Widget _buildInfoRow(String label, String value) {
     return Row(
       crossAxisAlignment: CrossAxisAlignment.start,
       children: [
         Text(
           label,
           style: const TextStyle(color: Colors.grey, fontSize: 12),
         ),
         Expanded(
           child: Text(
             value,
             style: const TextStyle(fontSize: 12),
           ),
         ),
       ],
     );
   }

  /// 构建费用信息卡片
  Widget _buildCostInfoCard() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildCostItem('总费用(元)', '20,000.67', Colors.black),
          _buildCostItem('欠费(元)', '10,000.67', Colors.red),
          _buildCostItem('押金(元)', '10,000', Colors.black),
          _buildCostItem('押交余额(元)', '0', Colors.black),
        ],
      ),
    );
  }

  /// 构建费用项
  Widget _buildCostItem(String label, String value, Color valueColor) {
    return Column(
      children: [
        Text(
          label,
          style: const TextStyle(fontSize: 12, color: Colors.grey),
        ),
        const SizedBox(height: 4),
        Text(
          value,
          style: TextStyle(fontSize: 14, color: valueColor, fontWeight: FontWeight.bold),
        ),
      ],
    );
  }

  /// 构建功能按钮区域
  Widget _buildFunctionButtons() {
    return Container(
      margin: const EdgeInsets.all(16),
      child: GridView.count(
        crossAxisCount: 4,
        mainAxisSpacing: 20,
        crossAxisSpacing: 20,
        children: [
          _buildFunctionButton(Icons.medical_services_outlined, '医嘱执行', Colors.blue),
          _buildFunctionButton(Icons.monitor_heart_outlined, '体征', Colors.green),
          _buildFunctionButton(Icons.description_outlined, '护理文书', Colors.purple),
          _buildFunctionButton(Icons.folder_outlined, '原始医嘱', Colors.orange),
          _buildFunctionButton(Icons.assignment_outlined, '出入量', Colors.cyan),
          _buildFunctionButton(Icons.email_outlined, '输液报告', Colors.indigo),
          _buildFunctionButton(Icons.location_on_outlined, '病房巡视', Colors.pink),
          _buildFunctionButton(Icons.note_outlined, '备忘录', Colors.amber),
          _buildFunctionButton(Icons.receipt_outlined, '费用清单', Colors.teal),
          _buildFunctionButton(Icons.sync_outlined, '流转记录', Colors.deepOrange),
          _buildFunctionButton(Icons.favorite_outline, '健康教育', Colors.brown),
        ],
      ),
    );
  }

  /// 构建功能按钮
  Widget _buildFunctionButton(IconData icon, String label, Color color) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: color.withOpacity(0.1),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Icon(icon, color: color),
        ),
        const SizedBox(height: 8),
        Text(
          label,
          style: const TextStyle(fontSize: 12),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}